<?php
namespace app\mypay\controller;
use think\Controller;
use think\Db;
use think\Loader;
class Huidiao extends Controller
{
    
    public function _initialize()
    {
        
        $config = cache('db_config_data');
        if (!$config)
        {
            $config = api('Config/lists');
            cache('db_config_data', $config);
        }
        config($config);
    }
    public function _empty($name)
    {
        return $this->fetch('/Public/404');
    }
  
    
    public function  wxnotify()
    {
       
        $zhifu = Db::name("zhifu")->where(['id' => 1])->find();
        $content = unserialize($zhifu['content']);
        Loader::import('weixinpay.WxPayPubHelper');
        $notify = new \Notify_pub();
        // $xml    = $GLOBALS['HTTP_RAW_POST_DATA'];
        $xml = file_get_contents('php://input');
        $notify->saveData($xml);
        if ($notify->checkSign() == FALSE)
        {
            $notify->setReturnParameter("return_code", "FAIL"); //返回状态码
            $notify->setReturnParameter("return_msg", "签名失败"); //返回信息
        }
        else
        {
            $notify->setReturnParameter("return_code", "SUCCESS"); //设置返回码
        }
        $returnXml = $notify->returnXml();
        echo $returnXml;
        $log_name = "text.log"; //log文件路径
        writefile($log_name, "【接收到的notify通知】:\n" . $xml . "\n");
        if ($notify->checkSign() == TRUE)
        {
            if ($notify->data["return_code"] == "FAIL")
            {
                writefile($log_name, "【通信出错】:\n" . $xml . "\n");
            }elseif ($notify->data["result_code"] == "FAIL")
            {
                writefile($log_name, "【业务出错】:\n" . $xml . "\n");
            }
            else
            {
                $shijian = date('Y-m-d H:i:s' , time());
                $out_trade_no = $notify->data["out_trade_no"]; //
                $weixin = $notify->data["transaction_id"]; //微信订单号
                $cymoney = $notify->data["total_fee"] / 100;
                $orderNo = $notify->data["out_trade_no"]; //我提交的订单号
                $openid  = $notify->data["openid"];//微信openid
                
                $order = Db::name("order")->where(['orderid' => $orderNo])->find();
                $userid = $order['userid'];
                
                
                $zyid = $order['zyid'];
                $ip = $order['ip'];
                $ddh = $orderNo;
                $qudao = $ddh;
                $ipz = Db::name("ip")->where(["ddh2" => $ddh])->find();
                if (!empty($ipz))
                {
                    exit;
                }
                if (config("dsklsz") == "1")
                {
                    $kl = Db::name("kou")->where(['userid' => $userid])->find();
                    $ns = $kl['ns'];
                    if (!empty($kl))
                    {
                        $cs = $kl['cs'] - 1;
                        if ($cs == "0")
                        {
                            $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            $name = $user['nickname'];
                            $data = [];
                            $data['name'] = $name;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh;
                            $data['qudao'] = $qudao;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['zymc'] = $zymc;
                            Db::name("kl")->insert($data);
                            Db::name("kou")->where(['userid' => $userid])->update(['cs' => $ns]); 
                            // 不扣量
                            // Db::name("siyou")->where(['id'=>$zyid])->setInc("cs");
                            Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs"); 
                            // //
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $openid;
                            
                           
                            $data['uopenid']  = $order['uopenid'];
                           
                           
                            Db::name("ip")->insert($data);
                        }
                        else
                        {
                            Db::name("kou")->where(['userid' => $userid])->setDec("cs"); 
                            // 写自己的数据库
                            $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                            if (empty($row))
                            { 
                                // /更新会员金额
                                $user = Db::name("member")->where(['id' => $userid])->find();
                                if (!empty($user))
                                {
                                    if ($cymoney < 1)
                                    {
                                        $money2 = $cymoney;
                                    }
                                    else
                                    {
                                        $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                    }
                                    $money = $user['money'] + $money2;
                                    Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                                }
                                $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                                $zymc = $shipin['name'];
                                if (!empty($shipin))
                                {
                                    Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                    Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs");
                                }
                                $data = [];
                                $data['zyid'] = $zyid;
                                $data['userid'] = $userid;
                                $data['zymc'] = $zymc;
                                $data['ddh'] = $ddh;
                                $data['money'] = $cymoney;
                                $data['shijian'] = $shijian;
                                $data['wxddh'] = $weixin;
                                Db::name("dingdan")->insert($data);
                                $ddh2 = substr($ddh, 0, 10);
                                $data = [];
                                $data['ip'] = $ip;
                                $data['userid'] = $userid;
                                $data['ddh'] = $ddh2;
                                $data['zyid'] = $zyid;
                                $data['ddh2'] = $ddh;
                                $data['zt'] = "已付";
                                $data['shijian'] = time();
                                $data['openid']  = $openid;
                               
                                $data['uopenid']  = $order['uopenid'];
                           
                                Db::name("ip")->insert($data);
                            } 
                            // 写自己的数据库
                        } 
                        // 不扣量
                        // 代理ID没添加扣量参数
                    }
                    else
                    { 
                        // 写自己的数据库
                        $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                        if (empty($row))
                        { 
                            // /更新会员金额
                            $user = Db::name("member")->where(['id' => $userid])->find();
                            if (!empty($user))
                            {
                                if ($cymoney < 1)
                                {
                                    $money2 = $cymoney;
                                }
                                else
                                {
                                    $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                                }
                                $money = $user['money'] + $money2;
                                Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                            }
                            $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                            $zymc = $shipin['name'];
                            if (!empty($shipin))
                            {
                                Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                                Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs");
                            }
                            $data = [];
                            $data['zyid'] = $zyid;
                            $data['userid'] = $userid;
                            $data['zymc'] = $zymc;
                            $data['ddh'] = $ddh;
                            $data['money'] = $cymoney;
                            $data['shijian'] = $shijian;
                            $data['wxddh'] = $weixin;
                            Db::name("dingdan")->insert($data);
                            $ddh2 = substr($ddh, 0, 10);
                            $data = [];
                            $data['ip'] = $ip;
                            $data['userid'] = $userid;
                            $data['ddh'] = $ddh2;
                            $data['zyid'] = $zyid;
                            $data['ddh2'] = $ddh;
                            $data['zt'] = "已付";
                            $data['shijian'] = time();
                            $data['openid']  = $openid;
                           
                            $data['uopenid']  = $order['uopenid'];
                            
                            Db::name("ip")->insert($data);
                        } 
                        // 写自己的数据库
                    } 
                    // 代理ID没添加扣量参数
                    // 扣量开关关闭
                }
                else
                {
                    $row = Db::name("dingdan")->where(['ddh' => $qudao])->find();
                    if (empty($row))
                    { 
                        // /更新会员金额
                        $user = Db::name("member")->where(['id' => $userid])->find();
                        if (!empty($user))
                        {
                            if ($cymoney < 1)
                            {
                                $money2 = $cymoney;
                            }
                            else
                            {
                                $money2 = $cymoney - (($cymoney * $user['txfeilv']) / 100);
                            }
                            $money = $user['money'] + $money2;
                            Db::name("member")->where(['id' => $userid])->update(['money' => $money]);
                        }
                        $shipin = Db::name("siyou")->where(['id' => $zyid])->find();
                        $zymc = $shipin['name'];
                        if (!empty($shipin))
                        {
                            Db::name("siyou")->where(['id' => $zyid])->setInc("cs");
                            Db::name("gongyou")->where(['id' => $shipin['pid']])->setInc("cs");
                        }
                        $data = [];
                        $data['zyid'] = $zyid;
                        $data['userid'] = $userid;
                        $data['zymc'] = $zymc;
                        $data['ddh'] = $ddh;
                        $data['money'] = $cymoney;
                        $data['shijian'] = $shijian;
                        $data['wxddh'] = $weixin;
                        Db::name("dingdan")->insert($data);
                        $ddh2 = substr($ddh, 0, 10);
                        $data = [];
                        $data['ip'] = $ip;
                        $data['userid'] = $userid;
                        $data['ddh'] = $ddh2;
                        $data['zyid'] = $zyid;
                        $data['ddh2'] = $ddh;
                        $data['zt'] = "已付";
                        $data['shijian'] = time();
                        $data['openid']  = $openid;
                      
                        $data['uopenid']  = $order['uopenid'];
                        
                        Db::name("ip")->insert($data);
                    } 
                    // 写自己的数据库
                } 
                // 扣量开关关闭
            }
        }
    }
    
}